package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.MrDeviceAlarmData;
import com.ruoyi.system.domain.vo.CurrentAlertsVO;
import com.ruoyi.system.mapper.MrDeviceAlarmDataMapper;
import com.ruoyi.system.service.IMrDeviceAlarmData2Service;
import com.ruoyi.system.service.IMrDeviceAlarmDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 设备报警数据Service业务层处理
 *
 * @author lsw
 * @date 2024-01-03
 */
@Service
public class MrDeviceAlarmDataServiceImpl implements IMrDeviceAlarmDataService {
    @Autowired
    private MrDeviceAlarmDataMapper mrDeviceAlarmDataMapper;

    @Autowired
    private IMrDeviceAlarmData2Service mrDeviceAlarmData2Service;

    @Autowired
    private RedisCache redisCache;


    /**
     * 查询设备报警数据
     *
     * @param id 设备报警数据主键
     * @return 设备报警数据
     */
    @Override
    public MrDeviceAlarmData selectMrDeviceAlarmDataById(String id) {
        return mrDeviceAlarmDataMapper.selectMrDeviceAlarmDataById(id);
    }

    /**
     * 查询设备报警数据列表
     *
     * @param mrDeviceAlarmData 设备报警数据
     * @return 设备报警数据
     */
    @Override
    public List<MrDeviceAlarmData> selectMrDeviceAlarmDataList(MrDeviceAlarmData mrDeviceAlarmData) {
        return mrDeviceAlarmDataMapper.selectMrDeviceAlarmDataList(mrDeviceAlarmData);
    }

    /**
     * 新增设备报警数据
     *
     * @param mrDeviceAlarmData 设备报警数据
     * @return 结果
     */
    @Override
    public int insertMrDeviceAlarmData(MrDeviceAlarmData mrDeviceAlarmData) {
        // 1. 生成ID
        mrDeviceAlarmData.setId(UUID.randomUUID().toString());
        // 4. 插入数据库
        return mrDeviceAlarmDataMapper.insertMrDeviceAlarmData(mrDeviceAlarmData);
    }

    /**
     * 修改设备报警数据
     *
     * @param mrDeviceAlarmData 设备报警数据
     * @return 结果
     */
    @Override
    public int updateMrDeviceAlarmData(MrDeviceAlarmData mrDeviceAlarmData) {
        return mrDeviceAlarmDataMapper.updateMrDeviceAlarmData(mrDeviceAlarmData);
    }

    /**
     * 批量删除设备报警数据
     *
     * @param ids 需要删除的设备报警数据主键
     * @return 结果
     */
    @Override
    public int deleteMrDeviceAlarmDataByIds(String[] ids) {
        return mrDeviceAlarmDataMapper.deleteMrDeviceAlarmDataByIds(ids);
    }

    /**
     * 删除设备报警数据信息
     *
     * @param id 设备报警数据主键
     * @return 结果
     */
    @Override
    public int deleteMrDeviceAlarmDataById(String id) {
        return mrDeviceAlarmDataMapper.deleteMrDeviceAlarmDataById(id);
    }

    /**
     * 查询设备报警数据列表
     *
     * @param mrDeviceAlarmData 设备报警数据
     * @return 设备报警数据集合
     */
    @Override
    public List<MrDeviceAlarmData> getDeviceAlarmDataList(MrDeviceAlarmData mrDeviceAlarmData) {
        List<MrDeviceAlarmData> deviceAlarmData;
        if ("1".equals(mrDeviceAlarmData.getDisplayAlarm())) {
            if (redisCache.hasKey(CacheConstants.DEV_WARN_100_1_CACHE)) {
                deviceAlarmData = redisCache.getCacheList(CacheConstants.DEV_WARN_100_1_CACHE);
            } else {
                deviceAlarmData = mrDeviceAlarmDataMapper.getDeviceAlarmDataList(mrDeviceAlarmData);
                redisCache.setCacheList(CacheConstants.DEV_WARN_100_1_CACHE, deviceAlarmData);
            }
        } else if ("2".equals(mrDeviceAlarmData.getDisplayAlarm())) {
            if (redisCache.hasKey(CacheConstants.DEV_WARN_100_2_CACHE)) {
                deviceAlarmData = redisCache.getCacheList(CacheConstants.DEV_WARN_100_2_CACHE);
            } else {
                deviceAlarmData = mrDeviceAlarmDataMapper.getDeviceAlarmDataList(mrDeviceAlarmData);
                redisCache.setCacheList(CacheConstants.DEV_WARN_100_2_CACHE, deviceAlarmData);
            }
        } else {
            deviceAlarmData = mrDeviceAlarmDataMapper.getDeviceAlarmDataList(mrDeviceAlarmData);
        }
        return deviceAlarmData;
    }

    @Override
    public List<CurrentAlertsVO> queryCurrentAlerts(MrDeviceAlarmData mrDeviceAlarmData) {
        return mrDeviceAlarmDataMapper.queryCurrentAlerts(mrDeviceAlarmData);
    }

    @Override
    public void deleteSixMonthsAgoData() {
        mrDeviceAlarmDataMapper.deleteSixMonthsAgoData();
    }

    /**
     * 查询当前设备报警数据列表
     *
     * @param mrDeviceAlarmData 设备报警数据
     * @return 当前设备报警数据集合
     */
    @Override
    public List<MrDeviceAlarmData> currentAlarm(MrDeviceAlarmData mrDeviceAlarmData) {
        return mrDeviceAlarmDataMapper.currentAlarm(mrDeviceAlarmData);
    }

    @Override
    public void insertBatch(List<MrDeviceAlarmData> records) {
        mrDeviceAlarmDataMapper.insertBatch(records);
    }

    /**
     * 数据库同步
     */
    @Override
    public void dataSynchronization() {
        //当前设备报警总数
        int count = mrDeviceAlarmDataMapper.countDeviceAlarm();
        //备用数据
        int count2 = mrDeviceAlarmData2Service.countDeviceAlarm();
        MrDeviceAlarmData mrDeviceAlarmData = new MrDeviceAlarmData();
        if (count > count2) {
            //获取数据
            List<MrDeviceAlarmData> mrDeviceAlarmDataList = mrDeviceAlarmData2Service.selectMrDeviceAlarmDataList(mrDeviceAlarmData);
            //获取需要更新的数据
            String[] ids = mrDeviceAlarmDataList.stream().map(MrDeviceAlarmData::getId).toArray(String[]::new);
            List<MrDeviceAlarmData> data = mrDeviceAlarmDataMapper.selectSyncMrDeviceAlarmDataList(ids);
            data = data.stream().filter(mr -> mr.getDeviceCode() != null && !mr.getDeviceCode().isEmpty()).collect(Collectors.toList());
            if (!data.isEmpty()) {
                //更新数据
                mrDeviceAlarmData2Service.insertBatch(data);
            }
        } else {
            //获取数据
            List<MrDeviceAlarmData> mrDeviceAlarmDataList = mrDeviceAlarmDataMapper.selectMrDeviceAlarmDataList(mrDeviceAlarmData);
            //获取需要更新的数据
            String[] ids = mrDeviceAlarmDataList.stream().map(MrDeviceAlarmData::getId).toArray(String[]::new);
            List<MrDeviceAlarmData> data = mrDeviceAlarmData2Service.selectSyncMrDeviceAlarmDataList(ids);
            data = data.stream().filter(mr -> mr.getDeviceCode() != null && !mr.getDeviceCode().isEmpty()).collect(Collectors.toList());
            if (!data.isEmpty()) {
                //更新数据
                mrDeviceAlarmDataMapper.insertBatch(data);
            }
        }
    }

}

